﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace Blip.UI.Controls
{
    /// <summary>
    /// Galeria obrazów.
    /// </summary>
    /// <seealso cref="PictureEventArgs"/>
    public partial class Gallery : SurfaceUserControl
    {
        /// <summary>
        /// Zdarzenie generowane po wybraniu obrazka.
        /// </summary>
        /// <seealso cref="PictureEventArgs"/>
        public event EventHandler<PictureEventArgs> Select;

        /// <summary>
        /// Zdarzenie generowane przy próbie zamknięcia galerii.
        /// </summary>
        public event EventHandler Cancel;

        /// <summary>
        /// Rozmiar miniaturki.
        /// </summary>
        protected const int SIZE = 100;

        /// <summary>
        /// Margines miniaturki wewnątrz przycisków.
        /// </summary>
        protected const int MARGIN = 3;

        /// <summary>
        /// Liczba kolumn miniaturek.
        /// </summary>
        protected const int ROWS = 3;

        /// <summary>
        /// Konstruktor inicjalizujący galerię.
        /// </summary>
        public Gallery()
        {
            InitializeComponent();
            DataContext = this;
        }

        /// <summary>
        /// Tytuł okna galerii wyświetlany u góry.
        /// </summary>
        public string Header { get; set; }

        private IEnumerable<string> collection;

        /// <summary>
        /// Lista ścieżek do plików obrazków.
        /// </summary>
        public IEnumerable<string> Collection 
        {
            set
            {
                collection = value;
                DisplayCollection();
            }
            get
            {
                if (collection == null)
                    collection = new List<string>();
                return collection;
            }
        }

        /// <summary>
        /// Zamraża przyciski.
        /// </summary>
        /// <returns>Obiekt, na rzecz którego wywoływana jest metoda</returns>
        public Gallery Freeze()
        {
            foreach (SurfaceButton button in gallery.Children)
                button.IsEnabled = false;

            cancelButton.IsEnabled = false;
            return this;
        }

        private void DisplayCollection()
        {
            gallery.RowDefinitions.Clear();

            int row = -1;
            int i = 0;
            foreach (var picture in collection)
            {
                if (i % ROWS == 0)
                {
                    gallery.RowDefinitions.Add(new RowDefinition());
                    ++row;
                }

                SurfaceButton button = new SurfaceButton();
                gallery.Children.Add(button);
                Grid.SetRow(button, row);
                Grid.SetColumn(button, i % ROWS);

                Image image = new Image();
                image.Stretch = Stretch.Uniform;
                image.Source = new BitmapImage(new Uri(picture));
                button.Margin = new Thickness(MARGIN);
                button.Width = SIZE;
                button.Height = SIZE;
                button.Click += new RoutedEventHandler(OnPictureSelect);
                button.Content = image;
                ++i;
            }
        }

        private void OnPictureSelect(object sender, RoutedEventArgs e)
        {
            if (Select != null)
            {
                SurfaceButton button = sender as SurfaceButton;
                Image image = button.Content as Image;
                BitmapImage bitmap = image.Source as BitmapImage;
                Select(this, new PictureEventArgs(bitmap.UriSource));
            }
        }

        private void OnCancelButtonHit(object sender, RoutedEventArgs e)
        {
            if (Cancel != null)
                Cancel(this, new EventArgs());
        }        
    }

    /// <summary>
    /// Argument zdarzenia agregujący wybrany obraz z galerii.
    /// </summary>
    /// <seealso cref="Gallery"/>
    public class PictureEventArgs : EventArgs
    {
        /// <summary>
        /// Konstruktor inicjalizujący obiekt argumentów zdarzenia.
        /// </summary>
        /// <param name="picture">Obraz przekazywany w zdarzeniu</param>
        public PictureEventArgs(Uri picture)
        {
            Picture = picture;
        }

        /// <summary>
        /// Zwraca wybrany obraz.
        /// </summary>
        public Uri Picture { get; private set; }
    }

}
